﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Utilities
{
    public class NamedTreeNode<T>
    {
        public virtual T Value { get; set; }
        public string Name;
        public List<NamedTreeNode<T>> Childs;
        public NamedTreeNode<T> Parent;

        public NamedTreeNode(string name, T value) : this(null, name, value)
        {
        }
        public NamedTreeNode(NamedTreeNode<T> parent, string name, T value)
        {
            Name = name;
            Value = value;
            Childs = new List<NamedTreeNode<T>>();
            Parent = parent;
        }

        public virtual NamedTreeNode<T> this[string path]
        {
            get
            {
                if (path.Contains('/') || path.Contains("\\"))
                {
                    string[] paths = path.Split('/', '\\');
                    NamedTreeNode<T> current = this;

                    for (int i = 0; i < paths.Length && current != null; i++)
                        current = current[paths[i]];

                    return current;
                }
                else
                {
                    if (Childs.Count(ntn => ntn.Name == path) == 1)
                        return Childs.Single(ntn => ntn.Name == path);
                    else
                        return null;
                }
            }
        }

        public override string ToString()
        {
            return Name + (Value == null ? "" : " : " + Value);
        }
    }
}
